package com.shangyueshang.controller.sys;

import com.shangyueshang.model.SysMenu;
import com.shangyueshang.request.IdDTO;
import com.shangyueshang.response.HttpResult;
import com.shangyueshang.service.SysMenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/5 19:39
 * @version: v1.0.0
 */
@RestController
@RequestMapping("/menu")
public class SysMenuController {
	
	@Resource
	private SysMenuService sysMenuService;
	
	/**
	 * 保存菜单
	 *
	 * @param sysMenu
	 * @return
	 */
	@PostMapping("/save")
	public HttpResult save(@RequestBody SysMenu sysMenu) {
		return HttpResult.ok(sysMenuService.saveOrUpdate(sysMenu));
	}
	
	/**
	 * 删除菜单
	 *
	 * @param ids
	 * @return
	 */
	@PostMapping("/delete")
	public HttpResult delete(@NotEmpty @RequestBody List<IdDTO> ids) {
		return HttpResult.ok(sysMenuService.removeByIds(ids.stream().map(IdDTO::getId).collect(Collectors.toList())));
	}
	
	/**
	 * 查询导航菜单树
	 *
	 * @param username
	 * @return
	 */
	@GetMapping("/findNavTree")
	public HttpResult findNavTree(@RequestParam String username) {
		return HttpResult.ok(sysMenuService.findTree(username, 1));
	}
	
	/**
	 * 查询菜单树
	 *
	 * @return
	 */
	@GetMapping("/findMenuTree")
	public HttpResult findMenuTree() {
		return HttpResult.ok(sysMenuService.findTree(null, 0));
	}
	
	
}
